/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes;

import com.android1.amarena2d.engine.GL;
import com.android1.amarena2d.texture.BitmapFont;
import com.badlogic.gdx.graphics.Color;


public class TextLabel extends BaseLeafEntity<FontNode> implements LifecycleObject {

    protected BitmapFont.HAlignment align = BitmapFont.HAlignment.LEFT;

    protected String text;
    protected int stringWidth;
    protected int zOrder;

    int alignmentWidth;
    boolean multiLine;

    public TextLabel() {
    }

    public TextLabel(String text) {
        this.text = text;
    }

    public TextLabel(String text, boolean multiLine) {
        this.text = text;
        this.multiLine = multiLine;
    }

    public TextLabel(String text, BitmapFont.HAlignment align) {
        this.text = text;
        this.align = align;
    }

    public TextLabel(String text, float x, float y) {
        this.text = text;
        position.x = x;
        position.y = y;
    }


    @Override
    public void render() {
        if (!active || !visible || text == null || text.length() == 0)
            return;


        final boolean isTranslate = transformDelegate != null;

        if (isTranslate)
            GL.pushMatrix();

        if (transformDelegate != null) {
            transformDelegate.apply();
        }

        if (!multiLine)
            parent.managedBitmapFont.getBitmapFont().draw(parent.spriteBatch, text, (int) getX(), (int) getY(), color);
        else
            parent.managedBitmapFont.getBitmapFont().drawMultiLineText(parent.spriteBatch, text, (int) getX(), (int) getY(), color, alignmentWidth, align);
//        if (align == Align.Center)
//            parent.spriteBatch.drawText(parent.font, text, (int) position.x - getStringWidth() / 2, (int) position.y, color);
//        else if (align == Align.Left)
//            parent.spriteBatch.drawText(parent.font, text, (int) position.x , (int) position.y, color);
//
        if (isTranslate)
            GL.popMatrix();
    }

    public String getText() {
        return text;
    }


    public TextLabel setText(String text) {
        this.text = text;
        this.stringWidth = 0;
        return this;
    }

    public TextLabel xy(float x, float y) {
        setXY(x, y);
        return this;
    }

    public TextLabel color(Color color) {
        setColor(color);
        return this;
    }

    public TextLabel color(final float r, final float g, final float b) {
        setColor(r, g, b);
        return this;
    }

    public TextLabel alpha(final float a) {
        setAlpha(a);
        return this;
    }


    public BitmapFont.HAlignment getAlign() {
        return align;
    }

    public TextLabel setAlign(BitmapFont.HAlignment align) {
        this.align = align;
        return this;
    }

    public boolean isMultiLine() {
        return multiLine;
    }

    public TextLabel setMultiLine(boolean multiLine) {
        this.multiLine = multiLine;
        return this;
    }

    public int getAlignmentWidth() {
        return alignmentWidth;
    }

    public TextLabel setAlignmentWidth(int alignmentWidth) {
        this.alignmentWidth = alignmentWidth;
        return this;
    }

    @Override
    public int getZOrder() {
        return zOrder;
    }

    @Override
    public void setZOrder(int zOrder) {
        this.zOrder = zOrder;
    }
}
